package main

import (
	"gorm.io/driver/sqlite"
	_ "gorm.io/driver/sqlite"
	"gorm.io/gorm"
)

type Want struct {
	gorm.Model // 嵌入gorm.Model，包含了ID, CreatedAt, UpdatedAt, DeletedAt字段

	Title   string   `gorm:"column:title;type:varchar(255);" json:Title`
	Answers []Answer `gorm:"foreignKey:WantID"`
}

type Answer struct {
	gorm.Model        // 支持软删除
	WantID     uint   `gorm:"type:int;index"`
	Answer     string `gorm:"type:text"`
}

func init() {
	db, err := sqliteDB()
	if err != nil {
		panic(err)
	}

	db.AutoMigrate(&Want{}, &Answer{})
}

func sqliteDB() (db *gorm.DB, err error) {
	return gorm.Open(sqlite.Open("sqlite.db"), &gorm.Config{})
}

func SqliteDB() *gorm.DB {
	db, err := sqliteDB()
	if err != nil {
		panic(err)
	}
	return db
}
